package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import CommonGui.AutoScaledPainter;
import CommonGui.ImagePrinter;
import DataObjects.Schedule;
import DataObjects.Table;
import Messages.MessageCheckScheduleIfExists;
import Messages.MessageCheckScheduleIfExistsReply;
import Messages.MessageSaveNewSchedule;
import Messages.MessageSavingReply;
import Messages.MessageUpdateSchedule;

public class PfChangeTableSchedule extends JPanel {

	private MainMenu mainMenu;
	private Vector<Schedule> schedules;
	private JPanel buttonsPanel;
	private JPanel status;
	private PaintSchedulePanel schedule;
	private  String [] schedulesName ;
	private JComboBox schedulesCB;
	private  VKTextField name;
	
	public PfChangeTableSchedule(MainMenu mm,Vector<Schedule> vector)
	{
		super();
		this.mainMenu=mm;
		this.schedules=vector;
		schedulesName = new String[schedules.size()+1];
		this.setLayout(new BorderLayout());
		initialize();
			
		
	}
	
	private void initialize()
	{
		schedulesName[0]="ריק";
		for (int i=0;i<schedules.size();i++)
		{
			schedulesName[i+1]=schedules.get(i).getScheduleName();
		}
		
		this.add(getButtonsPanel(), BorderLayout.EAST);
		schedule=new PaintSchedulePanel(mainMenu);
		this.add(schedule, BorderLayout.CENTER);
	}
	
	
	
	private JComboBox getSchedulesCB()
	{
		schedulesCB=new JComboBox(schedulesName);
		schedulesCB.addActionListener(new java.awt.event.ActionListener() 
	  	{
			public void actionPerformed(java.awt.event.ActionEvent e) {
				
				JComboBox scheduleBox = (JComboBox)e.getSource();
			    String selectedSchedule = (String)scheduleBox.getSelectedItem(); 
			    for(int i=0;i<schedules.size();i++)
			    {
			    	if(selectedSchedule.equals(schedules.get(i).getScheduleName()))
			    	{
			    		schedule.setVecTablesAndImages(schedules.get(i).getTables()) ;
			    		name.setText(selectedSchedule);			    		
			    	}
			    	
			    	if(selectedSchedule.equals("ריק"))
			    	{
			    		schedule.setVecTablesNull() ;
			    		name.setText("");			    		
			    	}
			    	
			    }
			    
			} 
		});	
		
	//	schedulesCB=new JComboBox();
		
		
		return schedulesCB;
	}
	private JPanel getButtonsPanel()
	{
		buttonsPanel=new JPanel();
		buttonsPanel.setBackground(new Color(245, 245, 245));
		buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.Y_AXIS));
		GridLayout grid =new GridLayout(0, 1, 5, 5);
		buttonsPanel.setLayout(grid);
		//buttonsPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
		buttonsPanel.setPreferredSize(new Dimension(150, 300));
		//buttonsPanel.add(Box.createVerticalGlue());
		buttonsPanel.add(getBtnPrint());
		buttonsPanel.add(getBtnClear());
		buttonsPanel.add(Box.createVerticalGlue());
		buttonsPanel.add(getSavePanel());
		buttonsPanel.add(getStatusPanel());
		return buttonsPanel;		
	}
	
	
	private JPanel getStatusPanel()
	{
		status=new JPanel();
		status.setBackground(new Color(245, 245, 245));
		status.setLayout(new BoxLayout(status, BoxLayout.Y_AXIS));
		return status;
	}
	private JPanel getSavePanel()
	{
		final JPanel savePanel=new JPanel();
		savePanel.setBackground(new Color(245, 245, 245));
		GridLayout grid2 =new GridLayout(0, 1, 5,5);
		savePanel.setLayout(grid2);
		savePanel.setPreferredSize (new Dimension(120,150));
		name =new VKTextField (mainMenu);
		name.setHorizontalAlignment(JTextField.RIGHT);
		name.setColumns(10);
		name.setMaximumSize( name.getPreferredSize() );
		JButton button=new JButton("שמור בשם :");
		button.setPreferredSize(new Dimension(120,30));
		savePanel.add(Box.createVerticalGlue());
		savePanel.add(getSchedulesCB());
		savePanel.add(button);
		savePanel.add(name);	
		savePanel.add(Box.createVerticalGlue());
		
		button.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {	
				if(schedule.getVecTables().size()!=0)
				{
				JButton btn=(JButton)e.getSource();
				status.removeAll();
				if (name.getText().isEmpty()) 
				{
					JLabel errorLabel=new JLabel("יש להקיש שם הסידור");
					JLabel mainLbl=new JLabel("הסידור לא נשמר");
					mainLbl.setFont(new Font("Tahoma", Font.BOLD, 11));
					mainLbl.setForeground(Color.red);
					errorLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
					errorLabel.setForeground(Color.red);
					status.add(mainLbl);
					status.add(errorLabel);
				}
				else 
				{
					String scheduleName=name.getText();
					try {
						
						
							Vector<Vector<String>> tableToSend=new Vector<Vector<String>>();         // ordered by : x,y,size,number,type,name
							Vector<Table> vect=schedule.getVecTables();
							
							for(int i=0;i<vect.size();i++)
							{
								Vector<String> vecString=new Vector<String>();
								vecString.add(Integer.toString(vect.get(i).getX()));
								vecString.add(Integer.toString(vect.get(i).getY()));
								vecString.add(Integer.toString(vect.get(i).getSize()));
								vecString.add(Integer.toString(vect.get(i).getNumber()));								
								vecString.add(vect.get(i).getType());
								vecString.add(scheduleName);
								tableToSend.add(vecString);							
							}

							MessageUpdateSchedule msgUpdateSchedule=new MessageUpdateSchedule(tableToSend);
							mainMenu.ms.client.sendToServer(msgUpdateSchedule);
							MessageSavingReply savingReply=(MessageSavingReply)mainMenu.ms.client.GetMessage();
							
								if(savingReply.getResult()==1)
								{
								   JLabel errorLabel=new JLabel("הסידור נשמר בהצלחה");
								   errorLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
								   errorLabel.setForeground(Color.blue);
								   status.add(errorLabel);								   
								   btn.setEnabled(false);								   
								}
								else
								{
									   JLabel errorLabel=new JLabel("בעיית תקשורת");
									   JLabel mainLbl=new JLabel("הסידור לא נשמר");
										mainLbl.setFont(new Font("Tahoma", Font.BOLD, 11));
										mainLbl.setForeground(Color.red);
										errorLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
										errorLabel.setForeground(Color.red);
										status.add(mainLbl);
									   status.add(errorLabel);								   
									   								   
									}
									
							
						
						
					}
					catch (IOException e1) {						
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
					
				}
					 
				}
				
				/*
				
				 JLabel errorLabel=new JLabel("יש להוסיף שולחן");
				   JLabel mainLbl=new JLabel("הסידור לא נשמר");
					mainLbl.setFont(new Font("Tahoma", Font.BOLD, 11));
					mainLbl.setForeground(Color.red);
					errorLabel.setFont(new Font("Tahoma", Font.BOLD, 11));
					errorLabel.setForeground(Color.red);
					status.add(mainLbl);
				   status.add(errorLabel);	
				   */
				mainMenu.getContentPane().validate();
			}
		});
		
		
		return savePanel;
	}
	
	
	private JButton getBtnClear()
	{
		JButton button=new JButton("ניקוי מסך");
		Icon clearIcon = new ImageIcon("clear-icon.png");
		Image img = ((ImageIcon) clearIcon).getImage() ;  
		Image newimg = img.getScaledInstance( 45, 45,  java.awt.Image.SCALE_SMOOTH ) ;  
		clearIcon = new ImageIcon( newimg );
		button=new JButton("ניקוי מסך",clearIcon); 
		button.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
	    button.setVerticalTextPosition(AbstractButton.BOTTOM);
		button.setHorizontalTextPosition(AbstractButton.CENTER);
		button.setPreferredSize(new Dimension (120,100));
		button.setSize(getMaximumSize());
		button.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {					
				mainMenu.getContentPane().remove(((BorderLayout)mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.CENTER));
				mainMenu.getContentPane().add(new PfNewTableSchedule(mainMenu), BorderLayout.CENTER);							
				mainMenu.getContentPane().validate();		        
			}
		});
		return button;
	}
	
	
	private JButton getBtnPrint()
	{
		JButton button=new JButton("הדפס");
		Icon printerIcon = new ImageIcon("printer-icon.png");
		Image img = ((ImageIcon) printerIcon).getImage() ;  
		Image newimg = img.getScaledInstance( 45, 45,  java.awt.Image.SCALE_SMOOTH ) ;  
	    printerIcon = new ImageIcon( newimg );
		button=new JButton("הדפסה",printerIcon);  
	    button.setVerticalTextPosition(AbstractButton.BOTTOM);
		button.setHorizontalTextPosition(AbstractButton.CENTER);
		button.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
		button.setPreferredSize(new Dimension(120, 100));
		button.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {	
				
				int w = schedule.getWidth();
			    int h = schedule.getHeight();
	//convert JPanel To Image
			    BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
			    Graphics2D g = bi.createGraphics();
			    schedule.paint(g);
			    ImageIcon img = new ImageIcon(bi);
		        Image scheduleToPrint=img.getImage();
	// printing Image 	        
		        AutoScaledPainter asp=  new AutoScaledPainter(scheduleToPrint);
		        ImagePrinter imgPrn=new ImagePrinter(asp);
		        imgPrn.DoPrint();
		        
			}
		});
		return button;
	}
	
    private VKTextField getNameTextField()
    {
    	VKTextField name =new VKTextField (mainMenu);
    	name.setHorizontalAlignment(JTextField.RIGHT);
    	
    	return name;
    }
}

	
	
	
	
	
	
	
	
	

